package com.msa.data;

import java.util.EmptyStackException;

/**
 * 栈
 *
 * @author 毕子航 951755883@qq.com
 * @date 2018/12/23
 */
public class MyStack {
	int[] data;
	int headIndex;

	public MyStack(int size) {
		if (size <= 0) {
			throw new IllegalArgumentException();
		}
		this.data = new int[size];
		this.headIndex = -1;
	}

	/**
	 * 压栈
	 *
	 * @param value
	 */
	public int push(int value) {
		if (full()) {
			throw new StackOverflowError();
		} else {
			data[++headIndex] = value;
		}
		return value;
	}

	/**
	 * 出栈
	 *
	 * @return
	 */
	public int pop() {
		int peek = peek();
		headIndex--;
		return peek;
	}

	/**
	 * 查看栈顶数据
	 *
	 * @return
	 */
	public int peek() {
		if (empty()) {
			throw new EmptyStackException();
		}
		return data[headIndex];
	}


	/**
	 * 是否是空栈
	 *
	 * @return
	 */
	public boolean empty() {
		return -1 == headIndex;
	}

	/**
	 * 是否已满
	 *
	 * @return
	 */
	public boolean full() {
		return data.length - 1 == headIndex;
	}
}
